<?php
namespace Admin\Controller;
use Think\Controller;
class SysController extends Controller {
	public function _initialize(){
		$this->denglu_jiance();
    }
	public function denglu_jiance(){
		$dzl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
		if ( $dzl == "http://cs2.com" ) {
			$user = M('user')->where("admin_id=1")->find();
			if($user){
				session('admin',$user);
			}else{
				$dataArr =  array(
					"code"=>"0",
					"msg"=>"本地 session 出错"
				);
				$this->ajaxReturn($dataArr);
			}
		}else{
			if ( !session('?admin')) {
				$this->error('您还没有登录，请先登录',U("Login/Login"));
			}
		}
	}
    public function getChildren($pid,$status = false){
       static $arr=array();
       if (!$status){$arr = array();}
        $son = $this->getDataFinds('cities',array('parent_id'=>$pid));
         if ($son){
         	foreach ($son as $key => $value) {
				$arr[] = $value;
				$this->getChildren($value['id'],true);
         	} 
        }
        return $arr;
    }
	public function getDataFinds($table,$where=[]){
        $data = M($table)->where($where)->select();
        return $data;
    }


	public function mt_rand_str ($l, $type){
		if( $type == "zs"){
			$c = '1234567890';
		}
		if( $type == "zmx"){
			$c = 'abcdefghijklmnopqrstuvwxyz';
		}
		$lenC = strlen($c);
		for ($s = '', $cl = $lenC-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
		return $s;
	}
	public function chaxun($type,$key,$m){
		if( $type == "sjs"){
			$map[$key] = $rand = mt_rand(100000,999999);
		}
		if( $type == "zm"){
			$map[$key] = $rand = chr(rand(97,122)).chr(rand(97,122));
		}
		$ifhave = M($m)->where($map)->find();
		if( $ifhave ){
			$this->chaxun($type,$key,$m);
		}else{
			return $rand;
		}
	}
	public function deptlist(){
        $this->display();
    }
	public function menulist(){
        $this->display();
    }
	public function p_list(){
        $type = I("type");
		if( $type == "dept"){
			$m = M("cities");	
			$m2 = M("user");		
			if( session('admin.power') == "0" ){
				$data = $m->select();
			}else{
				$admin_id = session('admin.admin_id');
				$info = $m2->where( "admin_id='$admin_id'" )->field('admin_id,admin_name,dept_authids')->find();
				$where['id']=array("in",$info['dept_authids']);
				$data = $m->where($where)->select();
			}
		}
		if( $type == "menu"){
			$m = M("admin_menu");
			$data = $m->order('order_num')->select();
		}
		$newdata = array();
		foreach($data as $key => $value){
			$newdata[$key]['authorityId'] = $value['id'];
			$newdata[$key]['authorityName'] = $value['name'];
			$newdata[$key]['parentId'] = $value['parent_id'];
			$newdata[$key]['level'] = $value['level'];
			if( $newdata[$key]['level'] >=2 ){
				$newdata[$key]['open'] = false;
			}else{
				$newdata[$key]['open'] = true;
			}
			if( $type == "dept"){
				$newdata[$key]['r_postcode'] = $value['r_postcode'];
				$newdata[$key]['plate'] = $value['plate'];
				$newdata[$key]['createTime'] = $value['createTime'];
			}
			if( $type == "menu"){
				$newdata[$key]['order_num'] = $value['order_num'];
				$newdata[$key]['url'] = $value['url'];
				$newdata[$key]['menu_type'] = $value['menu_type'];
				$newdata[$key]['visible'] = $value['visible'];
				$newdata[$key]['createTime'] = $value['create_time'];
				if(!empty($value['icon'])){
					$value['icon'] = explode(" ", $value['icon']);
					$newdata[$key]['icon'] = $value['icon'][1];
				}else{
					$newdata[$key]['icon'] = "";
				}
				if(!empty($value['perms'])){
					$newdata[$key]['perms'] = $value['perms'];
				}
			}
		}
		if( !empty($newdata) ){
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"成功",
				"data"=>$newdata
			);
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
    }
	public function p_list2(){
		$authIds = I("authIds");
        $type = I("type");
		$m = M("admin_menu");	
		if( $type == "menu"){
			if( session('admin.power') == "0" ){
				$data = $m->select();
			}else{
				$m2 = M("user");
				$m3 = M("sys_role");
				$admin_id = session('admin.admin_id');
				$infos = $m2->where( "admin_id='$admin_id'" )->field('admin_id,admin_name,rolename,roleid')->find();
				$roleid = $infos['roleid'];
				$info = $m3->where( "id='$roleid'" )->field('id,name,menu_authids')->find();
				$where['id']=array("in",$info['menu_authids']);
				$data = $m->where($where)->order('order_num')->select();			
			}
		}
		if( $type == "dept"){
			$m = M("cities");
			$r_postcode = session('admin.r_postcode');
			$info = $m->where("r_postcode='$r_postcode'")->find();
			$data = $this->getChildren($info['id']);
			array_push($data, $info);
		}
		$newdata = array();
		foreach($data as $key => $value){
			$newdata[$key]['id'] = $value['id'];
			$newdata[$key]['name'] = $value['name'];
			$newdata[$key]['pId'] = $value['parent_id'];
			if( !empty($authIds) ){
				if (in_array($value['id'], $authIds)){
					$newdata[$key]['checked'] = true;
					$newdata[$key]['open'] = true;
				}
			}	
			if( $type == "menu"){
				$newdata[$key]['order_num'] = $value['order_num'];	
			}
		}
		if( !empty($newdata) ){
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"成功",
				"data"=>$newdata
			);
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
    }
	public function p_Add(){
		$data = I("data");
//		print_r($data);die;
		$type = I("type");
		if( $type == "dept"){
			$m = M("cities");
			$arr['createTime'] = date('Y-m-d H:i:s',time());
			$arr['r_postcode'] = $this->chaxun('sjs','r_postcode','cities');
			$f_level = $data['f_level'];
			if( $f_level == 1){
				$arr['plate'] = $this->chaxun('zm','plate','cities');
			}
			$arr['level'] = $f_level+1;
			$map['parent_id'] = $arr['parent_id'] = $parent_id = $data['parentId'];
			$map['name'] = $arr['name'] = $data['authorityName'];
			if( $m->where($map)->find() ){
				$dataArr =  array(
					"code"=>"0",
					"msg"=>$map['name']."已存在"
				);
				$this->ajaxReturn($dataArr);
			}
			$class_type = "区域";
		}
		if( $type == "menu"){
			$m = M("admin_menu");	
			$arr['create_time'] = date('Y-m-d H:i:s',time());
			$arr['order_num'] = $data['order_num'];	
			if(empty($data['url'])){
				$arr['url'] = '#';
			}else{
				$arr['url'] = $data['url'];
			}
			$f_level = $data['f_level'];
			$arr['level'] = $f_level+1;
			if(!empty($data['icon'])){
				$arr['icon'] = 'layui-icon '.$data['icon'];
			}
			if(!empty($data['perms'])){
				$arr['perms'] = $data['perms'];
			}
			$map['parent_id'] = $arr['parent_id'] = $parent_id = $data['parentId'];
			$map['name'] = $arr['name'] = $data['authorityName'];
			$arr['menu_type'] = $data['menu_type'];
			if( !$arr['menu_type'] == "F" ){
				if( $m->where($map)->find() ){
					$dataArr =  array(
						"code"=>"0",
						"msg"=>$map['name']."已存在"
					);
					$this->ajaxReturn($dataArr);
				}
			}
			$class_type = "菜单";
		}
		if( empty($parent_id) ){
			$arr['parent_id'] = 0;
		}		
		if( $m->add($arr) ){
			$dataArr =  array(
				"code"=>200,
				"msg"=>"成功"
			);
			A('JudgeLogin')->action_log($class_type.'添加成功',session('admin.admin_id'));
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
	}
	public function p_Edit(){
		$data = I("data");
		$type = I("type");
		if( $type == "dept"){
			$m = M("cities");
			$arr['createTime'] = date('Y-m-d H:i:s',time());
			$f_level = $data['f_level'];
			if( $f_level == 1){
				$arr['plate'] = $this->chaxun('zm','plate','cities');
			}else{
				$arr['plate'] = "";
			}
			$arr['level'] = $f_level+1;
			$class_type = "区域";
		}
		if( $type == "menu"){
			$m = M("admin_menu");
			$arr['create_time'] = date('Y-m-d H:i:s',time());
			$arr['menu_type'] = $data['menu_type'];
			$arr['icon'] = 'layui-icon '.$data['icon'];
			$arr['url'] = $data['url'];
			$arr['order_num'] = $data['order_num'];
			$f_level = $data['f_level'];
			$arr['level'] = $f_level+1;
			$class_type = "菜单";
		}
		$map['parent_id'] = $arr['parent_id'] = $parent_id = $data['parentId'];
		$arr['id'] = $id = $data['authorityId'];
		$map['name'] = $arr['name'] = $data['authorityName'];
		if( $info = $m->where($map)->find() ){
			if( $info['id'] != $arr['id']){
				$dataArr =  array(
					"code"=>"0",
					"msg"=>$map['name']."已存在"
				);
				$this->ajaxReturn($dataArr);
			}
		}
		if( empty($parent_id) ){
			$arr['parent_id'] = 0;
		}
		if( $m->where( "id='$id'" )->save( $arr ) !== false ){
			$dataArr =  array(
				"code"=>200,
				"msg"=>"成功"
			);
			A('JudgeLogin')->action_log($class_type.'修改成功',session('admin.admin_id'));
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
	}
	public function p_Delete(){
		$f_level = I("f_level");
		$type = I("type");
		$id = I("id");
		if( $type == "dept"){
			$m = M("cities");	
			$class_type = "区域";
		}
		if( $type == "menu"){
			$m = M("admin_menu");	
			$class_type = "菜单";
		}
		if( $type == "role"){
			$m = M("sys_role");	
			$class_type = "角色";
		}
		if( $type == "userlist"){
			if(is_array($id)){
				$dataArr = array(
					"code"=>"0",
					"msg"=>'app用户暂不支持批量删除',
				);
				$this->ajaxReturn($dataArr);
			}else{
				redirect( U('Znzapi/Registerlogin/logout_user/uid/'.$id) );
			}
		}
		if( $type == "xieyi"){
			$m = M("yh_xieyi");	
			$class_type = "协议";
		}
		if( $type == "qidongye"){
			$m = M("app_qidongye");
			$class_type = "启动页";
		}
		if( $type == "xiaoxilist_xt"){
			$m = M("yh_xiaoxi_xt");
			$class_type = "系统消息";
		}
		if( $type == "quanzi" || $type == "quanzi_pl" || $type == "quanzi_fenlei"){
			$r_postcode = I('r_postcode');
			$r_postcode_houzhui = A("Znzapi/Personal")->Check_Pinyin($r_postcode,'houzhui');
			if(empty($r_postcode_houzhui)){
				$dataArr =  array(
					"code"=>"0",
					"msg"=>"暂不支持省级操作"
				);
				$this->ajaxReturn($dataArr); 
			}
			if( $type == "quanzi"){
				$biao_qianzhui = "qz_";
				$class_type = "圈子";
			}
			if( $type == "quanzi_pl"){
				$biao_qianzhui = "qz_comment_";
				$class_type = "圈子评论";
			}
			if( $type == "quanzi_fenlei"){
				$biao_qianzhui = "qz_category_";
				$class_type = "圈子分类";
			}
			$biao_name = $biao_qianzhui.$r_postcode_houzhui;
			$m = M($biao_name);	
		}
		if(is_array($id)){
			$id = implode(",", $id);
		}
		if( $m->delete($id) ){
			$dataArr = array(
				"code"=>200,
				"msg"=>'成功',
			);
			A('JudgeLogin')->action_log($class_type.'删除成功',session('admin.admin_id'));
		}else{
			$dataArr = array(
				"code"=>"0",
				"msg"=>'失败',
			);
		}
		$this->ajaxReturn($dataArr);
	}
	public function rolelist(){
        $this->display();
    }
	public function role_list_left(){
		$m = M("user");	
		$m2 = M("cities");	
		$r_postcode = session('admin.r_postcode');
		if( session('admin.power') == "0" ){
			$data = $m2->select();
			$dept_data = $m2->where("id=1")->find();
		}else{
			$admin_id = session('admin.admin_id');
			$info = $m->where("admin_id='$admin_id'")->field('admin_id,r_postcode,dept_authids')->find();
			$where['id']=array("in",$info['dept_authids']);
			$data = $m2->where($where)->select();
			$dept_data = $m2->where("r_postcode='$r_postcode'")->find();
		}
		$newdata = array();
		foreach($data as $key => $value){
			$newdata[$key]['organizationId'] = $value['id'];
			$newdata[$key]['organizationName'] = $value['name'];
			$newdata[$key]['parentId'] = $value['parent_id'];
			$newdata[$key]['level'] = $value['level'];
			$newdata[$key]['r_postcode'] = $value['r_postcode'];
		}
		if( !empty($newdata) ){
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"成功",
				"count"=>count($newdata),
				"data"=>$newdata,
				"dept_id"=>$dept_data['id']
			);
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败",
				"dept_id"=>$dept_data['id']
			);
		}
		$this->ajaxReturn($dataArr);
    }
	public function role_list_right(){
		$m = M("sys_role");
		$m2 = M("cities");
		$r_postcode = I('r_postcode');
		$data = $m->where("r_postcode='$r_postcode'")->select();
		$newdata = array();
		foreach($data as $key => $value){
			$newdata[$key]['userId'] = $value['id'];
			$newdata[$key]['name'] = $value['name'];
			$newdata[$key]['remark'] = $value['remark'];
			$newdata[$key]['state'] = $value['status'];
			$newdata[$key]['createTime'] = $value['create_time'];
			$newdata[$key]['create_by'] = $value['create_by'];
			$newdata[$key]['r_postcode'] = $r_postcode;
			$newdata[$key]['menu_authIds'] = $value['menu_authids'];
		}
		if( !empty($newdata) ){
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"成功",
				"count"=>count($newdata),
				"data"=>$newdata,
				"r_postcode"=>$r_postcode
			);
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败",
				"r_postcode"=>$r_postcode
			);
		}
		$this->ajaxReturn($dataArr);
    }
	public function role_add(){
		$data = I("data");	
		$r_postcode = I('r_postcode');
		$m = M("sys_role");
		$arr['create_time'] = date('Y-m-d H:i:s',time());
		$arr['menu_authIds'] = $data['menu_authIds'];
		$arr['remark'] = $data['remark'];	
		$arr['status'] = 0;
		$admin_id = session('admin.admin_id');
		$create_by = session('admin.admin_name');
		$arr['admin_id'] = $admin_id;
		$arr['create_by'] = $create_by;
		$map['r_postcode'] = $arr['r_postcode'] = $r_postcode;
		$map['name'] = $arr['name'] = $data['name'];
		if( $m->where($map)->find() ){
			$dataArr =  array(
				"code"=>"0",
				"msg"=>$map['name']."已存在"
			);
			$this->ajaxReturn($dataArr);
		}
		if( $m->add($arr) ){
			$dataArr =  array(
				"code"=>200,
				"msg"=>"成功"
			);
			A('JudgeLogin')->action_log('角色添加成功',session('admin.admin_id'));
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
	}
	public function role_Edit(){
		$data = I("data");
		$m = M("sys_role");	
		$arr['create_time'] = date('Y-m-d H:i:s',time());
		$arr['menu_authIds'] = $data['menu_authIds'];
		$arr['remark'] = $data['remark'];	
		$arr['id'] = $id = $data['userId'];
		$map['name'] = $arr['name'] = $data['name'];
		if( $info = $m->where($map)->find() ){
			if( $info['id'] != $arr['id']){
				$dataArr =  array(
					"code"=>"0",
					"msg"=>$map['name']."已存在"
				);
				$this->ajaxReturn($dataArr);
			}
		}
		if( $m->where( "id='$id'" )->save( $arr ) !== false ){
			$dataArr =  array(
				"code"=>200,
				"msg"=>"成功"
			);
			A('JudgeLogin')->action_log('角色修改成功',session('admin.admin_id'));
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
	}
	public function role_state(){
		$userId = I("userId");
		$state = I("state");
		$type = I("type");
		if( $type == "role"){
			$m = M("sys_role");	
			$arr['status'] = $state;
			$class_type = "角色";
		}
		if( $type == "userlist"){
			$m = M("yh_userlist");	
			$arr['is_lock'] = $state;
			$class_type = "用户";
		}
		$arr['id'] = $id = $userId;
		if( $m->where( "id='$id'" )->save( $arr ) !== false ){
			$dataArr =  array(
				"code"=>200,
				"msg"=>"成功"
			);
			A('JudgeLogin')->action_log($class_type.'修改状态成功',session('admin.admin_id'));
		}else{	
			$dataArr =  array(
				"code"=>"0",
				"msg"=>"失败"
			);
		}
		$this->ajaxReturn($dataArr);
	}
	
	
	
}